<%@ page import="ex01.Person" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%--
  Created by IntelliJ IDEA.
  User: wg
  Date: 2017/10/16
  Time: 13:55
  Description: el表达式的作用
--%>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" %>
<html>
<head>
    <title>EL表达式</title>
</head>
<body>
    <%--EL表达式的样式, ${表达式}--%>

    <%
        Person[] personArray = new Person[] {
                new Person(11, "wch", false),
                new Person(12, "wch", true)
        };
        Cookie ck = new Cookie("cookies_key", "cookies_value");
        response.addCookie(ck);
        // 添加类
        Person p = new Person(10, "wch", true);
        request.setAttribute("p1", p);

        // 添加数据
        request.setAttribute("p2", 1);

        // 添加数组
        request.setAttribute("p3", new int[] { 1, 2, 3, 4, 5, 6});
        request.setAttribute("p4", personArray);

        // 添加List
        List<Person> list = Arrays.asList(personArray);
        request.setAttribute("p5", list);

        // 添加Map
        Map<String, Person> map = new HashMap<>();
        map.put("p1", personArray[0]);
        map.put("p2", personArray[1]);
        request.setAttribute("p6", map);

        // page
        pageContext.setAttribute("p7", p);
    %>

    <%--
        快速的打印数据
        requestScope[key] 相当于request.getAttribute(key),提供的简略写法
        requestScope.key.beanName
        .name
        .age
        实际就是调用了对应的getName()和getAge()方法
        如果想要在jsp中使用el表达式，对象对应的类那么必须遵守java bean标准来定义：
        1. JavaBean 类必须是一个公共类，并将其访问属性设置为public, 如： public class Person{…}
        2. JavaBean 类必须有一个空的构造函数：类中必须有一个不带参数的公用构造器，例如：public Person() {…}
        3. javaBean 类变量都为private, 如： private int age;
        4. javaBean 属性是具有getter/setter方法的成员变量 也可以只提供getter方法，这样的属性叫只读属性；
        也可以只提供setter方法，这样的属性叫只写属性；如果属性类型为boolean类型，那么读方法的格式可以是get或is。
        例如名为abc的boolean类型的属性，它的读方法可以是getAbc()，也可以是isAbc()；
           -- 可以使用idea的自动生成功能，来生成对应的get/set方法
    --%>

    ${requestScope["p1"]}<br/>
    ${requestScope["p1"].name}<br/>
    ${requestScope["p1"].age}<br/>
    ${requestScope["p1"].man}<br/>

    <br>
    ${requestScope.p1.name}<br/>
    ${requestScope.p1.age}<br/>
    ${requestScope.p1.man}<br/>

    <br>
    ${requestScope.p2}

    <br>
    ${requestScope.p3}
    ${requestScope.p3[0]}
    ${requestScope.p3[1]}

    <br>
    ${requestScope.p4[0].man}
    ${requestScope.p4[1].man}
    ${requestScope.p4[3].man}
    ${requestScope.p4[0].name}

    <br>
    ${requestScope.p5}
    ${requestScope.p5[0].name}
    ${requestScope.p5[0].age}
    ${requestScope.p5[0].man}

    ${requestScope.p5[3].name}

    <br>
    ${requestScope.p6.p1.name}
    ${requestScope.p6.p1.man}
    ${requestScope.p6.p2.man}
    ${requestScope.p6.p3.man}

    <br>
    ${pageContext.response.characterEncoding}

    <br>
    ${pageScope.p7.name}

    <br>
    id = ${param.id},  x = ${param.x}

    <br>
    ${paramValues.id[0]}, ${paramValues.x[0]}, ${paramValues.x[1]}

    <br>
    ${header.host}
    ${header.cookie}

    <br>
    ${headerValues.host[0]}
    ${headerValues.cookie[0]}

    <br>
    <br>
    ${cookie.JSESSIONID.value}
    ${cookie.cookies_key.value}
</body>
</html>
